package com.zzf.msm.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.zzf.msm.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    //发送短信的方法
    @Override
    public boolean send(Map<String, Object> param, String phone) {
        if (StringUtils.isEmpty(phone)) return false;
        Credential cred = new Credential("AKIDmLdhU6AXR7YCbjpQ5XbaRddmlDpMAhT8",
                "qb6TcVAjw1rRq171iCVYLsCrKqQArSp4");//自己的腾讯key

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, "", clientProfile);

        SendSmsRequest req = new SendSmsRequest();

        String[] phoneNumberSet1 = {"+86" + phone};//电话
        String[] templateParamSet1 = {(String) param.get("code")};//验证码
        req.set("PhoneNumberSet", phoneNumberSet1);//电话
        req.set("TemplateParamSet", templateParamSet1);//验证码
        req.set("TemplateID", "926292");//模板
        req.set("SmsSdkAppid", "1400507972");//在添加应用看生成的实际SdkAppid
        req.set("Sign", "兔子编程公众号验证码");//签名

        try {
            SendSmsResponse resp = client.SendSms(req);//发送请求​
            return true;
        } catch (TencentCloudSDKException e) {
            return false;
        }
    }
}
